<?php
/**
 * 会员前台管理中心、账号管理、收藏操作类
 */

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('foreground');
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

class company extends foreground {

	private $comapny_db;
	private $companyimage_db;
	private $zhaobiao_reply_db;
	function __construct() {
		parent::__construct();
		$this->http_user_agent = str_replace('7.0' ,'8.0',$_SERVER['HTTP_USER_AGENT']);
		$this->certificate_db = pc_base::load_model('certificate_model');
		$this->comapny_db = pc_base::load_model('company_model');
		$this->companyimage_db = pc_base::load_model('companyimage_model');
	}

	public function init() {
		$memberinfo = $this->memberinfo;
		//初始化phpsso
		$phpsso_api_url = $this->_init_phpsso();
		//获取头像数组
		$avatar = $this->client->ps_getavatar($this->memberinfo['phpssouid']);

		$grouplist = getcache('grouplist');
		$memberinfo['groupname'] = $grouplist[$memberinfo[groupid]]['name'];

		include template('member', 'index');
	}
	/*处理企业信息*/
	function company_info(){
		$member = $this->comapny_db->get_one(array('userid'=>$this->memberinfo['userid']));
		$memberinfo = $this->memberinfo;
		if(isset($_POST['dosubmit']))
		{
			$data = $_POST['company'];
			if(is_array($member)){
				$this->comapny_db->update($data, array('userid'=>$this->memberinfo['userid']));
			}
			else
			{
				$data['userid']= $this->memberinfo['userid'];
				$this->comapny_db->insert($data);
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		}
		else
		{
			include template('member', 'company_info');
		}
	}
	function getarea(){
		$cities = getcache('3360', 'linkage');
		$data = $cities['data'];
		$id = $_POST['id'];
		$str = '<select>';
		if($id!=0){
			if(is_array($data))
			{
				foreach ($data as $k=>$v){
				
					if($v['parentid']!= $id || $v['parentid']==0){
						unset($data[$k]);
					}
				}
				
				foreach ($data as $key=>$val){
					$str .= '<option value="'.$val['name'].'">'.$val['name'].'</option>';
				}
				
				$str .= '</select>';
			}	
		}
		else
		{
			$str .= '<option vaule="">请选择</option>';	
		}
		$str .= '</select>';
		$data = array();
		echo $str;
	}
	
	function certificate(){
		$cer = $this->certificate_db->get_one(array('userid'=>$this->memberinfo['userid'],'catid'=>1));
			
		$cer2 = $this->certificate_db->get_one(array('userid'=>$this->memberinfo['userid'],'catid'=>2));
		include template('member', 'company_certificate');
	}
	function addcertificate(){
		$catid = intval($_GET['catid']);
		if(isset($_POST['dosubmit']))
		{
			$data = $_POST['info'];
			$data['userid']= $this->memberinfo['userid'];
			$this->certificate_db->insert($data);
			showmessage(L('operation_success'), HTTP_REFERER);	
		}else{
			$cer = $this->certificate_db->get_one(array('userid'=>$this->memberinfo['userid'],'catid'=>1));
			
			$cer2 = $this->certificate_db->get_one(array('userid'=>$this->memberinfo['userid'],'catid'=>2));
		}
		include template('member', 'company_add_certificate');
	}
	function delcertificate(){
		$cid = intval($_GET['cid']);
		$this->certificate_db->delete('cid='.$cid);
		showmessage(L('operation_success'), HTTP_REFERER);	
	}
	//企业形象
	function company_image(){
		if($_POST['dosubmit']){
			$data= $_POST['info'];
			$get_data = $this->companyimage_db->get_one('uid='.$this->memberinfo['userid']);
			if($get_data)
			{
				$this->companyimage_db->update($data, array('uid'=>$this->memberinfo['userid']));	
			}
			else
			{
				$data['uid']=$this->memberinfo['userid'];
				$this->companyimage_db->insert($data);
			}
		}else{
			$data = $this->companyimage_db->get_one('uid='.$this->memberinfo['userid']);
		}
		include template('member', 'company_image');
	}
	/**
	 * 
	 * 业主发起对公司的签约列表
	 */
	function signed(){
		$memberinfo = $this->memberinfo;
		$userid = $memberinfo['userid'];
		$this->signed_db = pc_base::load_model('signed_model');
		$table_pre = $this->signed_db->db_tablepre;
		$member_company_db = $table_pre.'member_company';
		$signed_db = $table_pre.'signed';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 15;
		$offset = intval($pagesize*($page-1));
		$signedid = intval($_GET['signedid']);
		if($signedid){$con = ' and signedid='.$signedid;}
			$sql = 'select a.*,b.companyname from '.$signed_db.' as a left join 
			'.$member_company_db.' as b on a.userid=b.userid where a.c_userid = '.$userid.$con;
			$sql_count = 'select count(a.signedid) as num from '.$signed_db.' as a left join 
			'.$member_company_db.' as b on a.userid=b.userid where a.c_userid = '.$userid.$con;
		
		$sql .= " LIMIT $offset,$pagesize";
		
		$this->signed_db->query($sql);
		$signed_data = $this->signed_db->fetch_array();
		$this->signed_db->query($sql_count);
		$total = $this->signed_db->fetch_array();
		
		$total = $total[0]['num'];
		
		$pages = pages($total, $page, $pagesize);
		include template('member', 'company_signed');
	}
	//公司删除签约
	function del_signed(){
		$signedid = $_GET['signedid'];
		if(intval($signedid)<1) return ;
		$this->signed_db = pc_base::load_model('signed_model');
		$s = $this->signed_db->delete('signedid='.$signedid);
		$msg =  $s>0 ? '删除成功':'删除失败'; 
		showmessage($msg, HTTP_REFERER);	
	}
	/**
	 * 
	 * 留言咨询管理
	 */
	function message(){

		$this->messages_db = pc_base::load_model('messages_model');
		
		$table_pre = $this->messages_db->db_tablepre;
		$member_company_db = $table_pre.'member_company';
		$message_db = $table_pre.'messages';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 15;
		$offset = intval($pagesize*($page-1));
		
	$sql = 'SELECT a.userid,a.content,a.addtime,a.messageid,a.replyid,a.name, b.content as rcontent,b.replyid as rid 
FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_company_db.' AS c ON c.userid = a.userid 
WHERE a.userid ='.$this->memberinfo['userid'].' and b.replyid is null ';
			//$sql = 'select a.* from '.$message_db.' as a left join 
			//'.$member_company_db.' as b on a.companyid=b.companyid where a.companyid = //'.intval($_GET['companyid']);
			$sql_count = 'select count(a.messageid) as num FROM '.$message_db.' AS a
LEFT JOIN '.$message_db.' AS b ON a.messageid = b.replyid
LEFT JOIN '.$member_company_db.' AS c ON c.userid = a.userid
WHERE a.userid ='.$this->memberinfo['userid'].' and b.replyid is null';
		
		$sql .= " LIMIT $offset,$pagesize";
		//echo $sql;exit;
		$this->messages_db->query($sql);
		$message_data = $this->messages_db->fetch_array();
	
		$this->messages_db->query($sql_count);
		$total = $this->messages_db->fetch_array();
		
		$total = $total[0]['num'];
		
		$pages = pages($total, $page, $pagesize);
		
		$data = $this->company_data;
		include template('member', 'company_message');
	}
	///确定和业主签约
	function sure_signed(){
		$signedid = intval($_GET['signedid']);
		if($signedid<1) return '';
		$this->signed_db = pc_base::load_model('signed_model');
		$result = $this->signed_db->update('is_sure=1', 'signedid='.$signedid);
		$msg = $result==1?'同意签约成功，你可以给该业主发感谢站内信':'签约失败';
		showmessage($msg, HTTP_REFERER);
	}
	//业主对公司的评价
	function appraisal(){
		$this->signed_db = pc_base::load_model('signed_model');
		if($_POST['dosubmit']){
			$data = $_POST['rcontent'];
			$signedid = $_POST['signedid'];
			$result = $this->signed_db->update('rcontent=\''.$data.'\'', 'signedid='.$signedid);
			$msg = $result==1?'回复成功':'回复失败';
			showmessage($msg, HTTP_REFERER);
		}else{
			$signedid = intval($_GET['signedid']);
			if($signedid<1) return '';
			$signed_data = $this->signed_db->get_one('signedid='.$signedid);
			include template('member', 'company_appraisal');
		}
	}
	//评价列表
	function appraisal_list(){
		$this->signed_db = pc_base::load_model('signed_model');
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 10;
		$offset = intval($pagesize*($page-1));
	
		$sql = 'SELECT *  FROM '.$this->signed_db->table_name.' WHERE c_userid='.$this->memberinfo['userid'].' order by signedid desc ';

		$sql_count = 'select count(signedid) as num FROM '.$this->signed_db->table_name.' WHERE c_userid='.$this->memberinfo['userid'].' order by signedid desc ';
		
		$sql .= " LIMIT $offset,$pagesize ";
		
		$this->signed_db->query($sql);
		$pj_data = $this->signed_db->fetch_array();
		//print_r($pj_data);exit;
		$this->signed_db->query($sql_count);
		$total = $this->signed_db->fetch_array();
		$total = $total[0]['num'];
		$pagesbar = pages($total, $page, $pagesize);
		include template('member', 'company_appraisal_list');
	}
	//公司投标
	function zhaobiao_reply(){
		$this->zhaobiao_reply_db = pc_base::load_model('zhaobiao_reply_model');
		if($_POST['dosubmit']){
			$_POST['info'] = str_replace(APP_PATH, '', $_POST['info']);
			$data = $_POST['info'];
			$data['quotation'] = addslashes($_POST['info']['quotation']);
			$data['inputtime'] = strtotime('now');
			
			$result = $this->zhaobiao_reply_db->add_relpy($data);

			$msg = $result==1?'回复成功':'回复失败';
			showmessage($msg, HTTP_REFERER);
		}
	}
}
?>